package com.google.project.client.ProjectManager;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.project.client.Service.AssignTaskService;
import com.google.project.client.Service.AssignTaskServiceAsync;
import com.google.project.client.Service.ProjTaskService;
import com.google.project.client.Service.ProjTaskServiceAsync;
import com.google.project.shared.ProjectStaffDTO;
import com.google.project.shared.ProjectTaskDTO;

public class AssignTaskDialog extends DialogBox {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField ListBox cboTask;
	@UiField Button btnClose;
	@UiField Button button;


	interface Binder extends UiBinder<Widget, AssignTaskDialog> {
	}
	private final ProjTaskServiceAsync projectTaskService = GWT.create(ProjTaskService.class);
	private final AssignTaskServiceAsync assignService = GWT.create(AssignTaskService.class);
	private List<ProjectStaffDTO> listStaff= new ArrayList<ProjectStaffDTO>();
	private List<ProjectTaskDTO> listTask= new ArrayList<ProjectTaskDTO>();
	public AssignTaskDialog() {
		setWidget(binder.createAndBindUi(this));
	}
	public AssignTaskDialog(List<ProjectStaffDTO> data,String projectId) {
		setWidget(binder.createAndBindUi(this));
		listStaff=data;
		projectTaskService.getListOrderTaskByProjectId(projectId, new AsyncCallback<List<ProjectTaskDTO>>() {
			
			@Override
			public void onSuccess(List<ProjectTaskDTO> result) {
				listTask=result;
				// TODO Auto-generated method stub
				cboTask.addItem("Lựa chọn", "-1");
				for(ProjectTaskDTO task: result)
				{
					if(task.getIsMile()==0&&task.getGroup()!=1&&task.getStatus()!=3&&task.getStatus()!=2)
					{
						cboTask.addItem(task.getTaskName(), task.getId());
					}
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	void close()
	{
		this.hide();
	
	}
	
	public ProjectTaskDTO getFromList(String taskId, List<ProjectTaskDTO> listTask) {
		if (listTask != null && listTask.size() > 0) {
			for (int i = 0; i < listTask.size(); i++) {
				if (listTask.get(i).getId().equals(taskId)) {
					return listTask.get(i);
				}
			}
		}
		return null;

	}
	@UiHandler("btnClose")
	void onBtnCloseClick(ClickEvent event) {
		close();
		PMEntrypoint.get().PMProjectDetail(listTask.get(0).getProjectId());
	}
	@UiHandler("button")
	void onButtonClick(ClickEvent event) {
		
		if(cboTask.getValue(cboTask.getSelectedIndex()).equals("-1"))
		{
			Window.alert("Bạn phải chọn công việc để giao");
		}
		else
		{
			Boolean check = true;
			ProjectTaskDTO task = getFromList(cboTask.getValue(cboTask.getSelectedIndex()), listTask);
			for(ProjectStaffDTO staff:listStaff)
			{
				if(task.getDueDate()>staff.getFinishtDate()||(task.getStatus()==0&&task.getStartDate()<staff.getStartDate()))
				{
					 check = false;
					 break;
				}
			}
			if(check)
			{
				assignService.addStaff(listStaff, task.getId(), new AsyncCallback<Void>() {
					
					@Override
					public void onSuccess(Void result) {
						// TODO Auto-generated method stub
						Window.alert("Thực hiện thành công");
						close();
						PMEntrypoint.get().PMProjectDetail(listTask.get(0).getProjectId());
					}
					
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}
				});
			}
			else
			{
				Window.alert("Không thể giao việc vì có nhân viên không thể tham gia vào thời gian công việc triển khai");
			}
		}
	}
}
